<?php

	#doc
	#	classname:	AlertController	
	#	scope:		PUBLIC
	#
	#/doc
	
	class AlertController extends CController
	{
		#	internal variables
		
		#	Constructor
		
		
		public function actionList ()
		{
		$salery=array('Жилая недвижимость','Земельная недвижимость','Гаражи, парковки','Коммерческая недвижимость');
		$type=array('Куплю','Продам','Сдам','Сниму');
		$count=array();
		$alert_land= new alert_land();
		for ( $i=1; $i < 7; $i++ )
		{ 
			foreach( $type as $key)
			{
			
			
						$co=alert_land::model()->findAll('type_salery=:type_salery AND number_room=:number_room AND type=:type',array(':type_salery'=>$key,':number_room'=>$i,'type'=>'Жилая недвижимость'));
				$count[$i][$key]=count($co);
			
			}
		
		}
		
		foreach( $salery as $sal)
		{
		
		foreach( $type as $key)
			{


						
				$count_all[$sal][$key]=alert_land::model()->count('type_salery=:type_salery AND type=:type',array(':type_salery'=>$key,'type'=>$sal));

			}
				
		}
		
		//Земельная недвижимость
		
		for ( $i=1; $i < 7; $i++ )
		{ 
			foreach( $type as $key)
			{


						$co=alert_land::model()->findAll('type_salery=:type_salery AND number_room=:number_room AND type=:type',array(':type_salery'=>$key,':number_room'=>$i,'type'=>':Земельная недвижимость'));
				$count_land[$i][$key]=count($co);
			}

		}
		
		//Гаражи парковки
		
			for ( $i=1; $i < 7; $i++ )
		{ 
			foreach( $type as $key)
			{


						$co=alert_land::model()->findAll('type_salery=:type_salery AND number_room=:number_room AND type=:type',array(':type_salery'=>$key,':number_room'=>$i,'type'=>':Гаражи, парковки'));
				$count_gar[$i][$key]=count($co);
			}

		} 
		
		//Комерческая недвижимость
			for ( $i=1; $i < 7; $i++ )
		{ 
			foreach( $type as $key)
			{


						$co=alert_land::model()->findAll('type_salery=:type_salery AND number_room=:number_room AND type=:type',array(':type_salery'=>$key,':number_room'=>$i,':type'=>'Комерческая недвижимость'));
				$count_com[$i][$key]=count($co);
			}

		}
		
		//Дома котеджи
		
		foreach( $type as $key)
			{


						$co=alert_land::model()->findAll('type_salery=:type_salery AND type=:type',array(':type_salery'=>$key,':type'=>'Дома,котеджи'));
				$count_cot[$key]=count($co);
			}
		
			$this->render('list',array('count'=>$count,'type'=>$type,'count_land'=>$count_land,'count_com'=>$count_com,'count_gar'=>$count_gar,'count_cot'=>$count_cot,'count_all'=>$count_all));
		}
		
		public function actionAdd ()
		{
			$this->render('Add');
		}
		
		public function actionSave ( )
		{
			if ( (isset($_POST['type']))&&(isset($_POST['adress']))&&(isset($_POST['phone']))&&(isset($_POST['type_salery']))&&(isset($_POST['number_room'])) )
			{
				$alert_land= new alert_land();
				$alert_land->type=$_POST['type'];
					$alert_land->number_room=$_POST['number_room'];
						$alert_land->price=$_POST['price'];
							$alert_land->area=$_POST['area'];
								$alert_land->adress=$_POST['adress'];
							$alert_land->flor=$_POST['flor'];
						$alert_land->phone=$_POST['phone'];
					$alert_land->type_salery=$_POST['type_salery'];			
					if ( $alert_land->save() )
					{
						$this->redirect('/alert/list');
					}				
			}
		}
		
		public function actionView()
		{
			$sp1=$_GET['type'];
			$sp=split(':',$sp1);
			if (!isset($sp[1]))
			{
				$spisok=alert_land::model()->findAll('type=:type',array(':type'=>$sp[0]));
			}
			elseif (!isset($sp[2])  )
			{
				$spisok=alert_land::model()->findAll('type=:type AND number_room=:number_room',array(':type'=>$sp[0],':number_room'=>$sp[1]));
			}
			elseif (!isset($sp[3]))
			{
				$spisok=alert_land::model()->findAll('type=:type AND number_room=:number_room AND type_salery=:type_salery',array(':type'=>$sp[0],':number_room'=>$sp[1],':type_salery'=>$sp[2]));
			}
			$this->render('view',array('spisok'=>$spisok,'put'=>$sp));
		}
	
	}
	###

?>